import {handlerGobackClick, hint} from "../../../utils/util";
import {Renter} from "../../../models/renter";

const renter = new Renter()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        arr: [{
            tenantName: '',
            rentantIdcard: '',
            tenantPhone: '',
        }],
        startTime: false,
        endTime: false,
        startTimeValue:"",
        endTimeValue: "",
        minDate: new Date(2020, 0, 1).getTime(),
        maxDate: new Date(2050, 0, 1).getTime(),
        currentDate: '12:00',
        currentDate1: '12:00',
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            } else if (type === 'month') {
                return `${value}月`;
            }
            return value;
        },
        commId:'',
        commIdName:'',
        buildingId:'',
        buildingIdName:'',
        housId:'',
        housIdName:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        console.log(new Date(2020, 0, 1).getTime());
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    handlerGobackClick,
    addRent() {
        let arr = [...this.data.arr]
        arr.push({
            tenantName: '',
            rentantIdcard: '',
            tenantPhone: '',
        })
        this.setData({
            arr: arr
        })
    },
    controllPopup(e) {
        let key = e.currentTarget.dataset.key
        this.setData({
            [key]: !this.data[key]
        })
    },
    selectedTime(event) {
        let {key,value} = event.currentTarget.dataset
        this.setData({
            [key]:!this.data[key],
            [value]:event.detail
        })
    },
    onChange(e) {
        let arr = JSON.parse(JSON.stringify(this.data.arr))
        let {key, index} = e.currentTarget.dataset
        arr[index][key] = e.detail
        this.setData({
            arr:arr
        })
    },
    goApply(){
        console.log(this.data.arr);
        if(!this.data.startTimeValue){
            hint('请选择租期开始时间')
            return
        }
        if(!this.data.endTimeValue){
            hint('请选择租期结束时间')
            return
        }
        if(!this.data.commId){
            hint('请选择小区')
            return
        }
        if(!this.data.buildingId){
            hint('请选择楼号')
            return
        }
        if(!this.data.housId){
            hint('请选择房号')
            return
        }
        renter.add(this.data.arr,this.data.housId,this.data.commId,this.data.startTimeValue,this.data.endTimeValue)
            .then((res)=>{
                hint(res.message)
                wx.redirectTo({
                    url:'/owner-app/pages/rent-house/index'
                })
            })
            .catch((err)=>{
                console.log(err);})
    },
    goSelectItem(){
        wx.navigateTo({
            url: `/owner-app/pages/select-community/index?type=renterSelectCommunity&commid=${this.data.commId}`
        })
    },
    goSelectItem1(){
        if(!this.data.commId){
            hint('请选择小区！')
            return
        }
        wx.navigateTo({
            url: `/owner-app/pages/select-community/index?type=renterSelectBuilding&phone=${this.data.commId}&buildingId=${this.data.buildingId}`
        })
    },
    goSelectItem2(){
        if(!this.data.buildingId){
            hint('请选择楼号！')
            return
        }
        wx.navigateTo({
            url: `/owner-app/pages/select-community/index?type=renterSelectHous&phone=${this.data.buildingId}&housId=${this.data.housId}`
        })
    }
})







